﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Drawing;

public partial class Orders : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection();

    #region Code Order
    void LoadOrderInfo()
    {
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "Sales.Orders_get";
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        gvOrders.AutoGenerateSelectButton = true;
        gvOrders.DataSource = dt;
        gvOrders.DataBind();
    }

    void Loadallcombobox()
    {
        SqlDataAdapter da = new SqlDataAdapter("SELECT custid,companyname FROM Sales.Customers", con);
        DataTable dt = new DataTable("Customers");
        da.Fill(dt);
        ddlOrderCustomer.DataValueField = "custid";
        ddlOrderCustomer.DataSource = dt;
        ddlOrderCustomer.DataTextField = "companyname";
        ddlOrderCustomer.DataBind();
        da = new SqlDataAdapter("SELECT empid,firstname+' '+lastname AS fullname  FROM HR.Employees", con);
        dt = new DataTable("Employees");
        da.Fill(dt);
        ddlOrderEmployee.DataValueField = "empid";
        ddlOrderEmployee.DataSource = dt;
        ddlOrderEmployee.DataTextField = "fullname";
        ddlOrderEmployee.DataBind();
        da = new SqlDataAdapter("SELECT shipperid, companyname  FROM Sales.Shippers", con);
        dt = new DataTable("Shipper");
        da.Fill(dt);
        ddlShipperid.DataValueField = "shipperid";
        ddlShipperid.DataSource = dt;
        ddlShipperid.DataTextField = "companyname";
        ddlShipperid.DataBind();
    }

    void defaultOrderform()
    {
        txtOrderID.Text = "";
        ddlOrderCustomer.SelectedIndex = 0;
        ddlOrderEmployee.SelectedIndex = 0;
        txtOrderDate.Text = DateTime.Now.ToString("yyyy/MM/dd");
        txtRequiredDate.Text = DateTime.Now.ToString("yyyy/MM/dd");
        txtShippedDate.Text = DateTime.Now.ToString("yyyy/MM/dd");
        //cmbShipperid.SelectedIndex = 0;
        txtFreight.Text = "0.01";
        txtShipname.Text = "";
        txtShipaddress.Text = "";
        txtShipcity.Text = "";
        txtShipRegion.Text = "";
        txtShipcode.Text = "";
        txtSearch.Text = "";
        ddlShipCountry.SelectedIndex = -1;
        btnAdd.Enabled = true;
        btnUpdate.Enabled = false;
        btnDelete.Enabled = false;
        ckbShipperDate.Enabled = true;
        ckbShipperDate.Checked = false;
    }

    void InsertnewOrder()
    {
        try
        {
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "Sales.Orders_Insert";
            SqlParameter param = new SqlParameter("@custid", SqlDbType.Int);
            param.Value = int.Parse(ddlOrderCustomer.SelectedValue.ToString());
            cmd.Parameters.Add(param);
            param = new SqlParameter("@empid", SqlDbType.Int);
            param.Value = int.Parse(ddlOrderEmployee.SelectedValue.ToString());
            cmd.Parameters.Add(param);
            DateTime dt;
            param = new SqlParameter("@orderdate", SqlDbType.DateTime);
            DateTime.TryParse(txtOrderDate.Text, out dt);
            param.Value = dt.ToShortDateString();
            cmd.Parameters.Add(param);
            param = new SqlParameter("@requireddate", SqlDbType.DateTime);
            DateTime.TryParse(txtRequiredDate.Text, out dt);
            param.Value = dt.ToShortDateString();
            cmd.Parameters.Add(param);
            param = new SqlParameter("@shippeddate", SqlDbType.DateTime);
            if (ckbShipperDate.Checked == true)
            {
                DateTime.TryParse(txtShippedDate.Text, out dt);
                param.Value = dt.ToShortDateString();
            }
            else
            {
                param.Value = System.Convert.DBNull;
            }
            cmd.Parameters.Add(param);
            param = new SqlParameter("@shipperid", SqlDbType.Int);
            param.Value = int.Parse(ddlShipperid.SelectedValue.ToString());
            cmd.Parameters.Add(param);
            param = new SqlParameter("@freight", SqlDbType.Money);
            param.Value = decimal.Parse(txtFreight.Text);
            cmd.Parameters.Add(param);
            param = new SqlParameter("@shipname", SqlDbType.NVarChar, 40);
            param.Value = txtShipname.Text;
            cmd.Parameters.Add(param);
            param = new SqlParameter("@shipaddress", SqlDbType.NVarChar, 60);
            param.Value = txtShipaddress.Text;
            cmd.Parameters.Add(param);
            param = new SqlParameter("@shipcity", SqlDbType.NVarChar, 15);
            param.Value = txtShipcity.Text;
            cmd.Parameters.Add(param);
            param = new SqlParameter("@shipregion", SqlDbType.NVarChar, 15);
            param.Value = txtShipRegion.Text;
            cmd.Parameters.Add(param);
            param = new SqlParameter("@shippostalcode", SqlDbType.NVarChar, 10);
            param.Value = txtShipcode.Text;
            cmd.Parameters.Add(param);
            param = new SqlParameter("@shipcountry", SqlDbType.NVarChar, 15);
            param.Value = ddlShipCountry.Text;
            cmd.Parameters.Add(param);
            cmd.ExecuteNonQuery();
            Response.Write("<script language='JavaScript'> alert('Insert successful!'); </script>");
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            con.Close();
        }
    }

    void UpdateOrder()
    {
        try
        {
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "Sales.Orders_Update";
            SqlParameter param = new SqlParameter("@orderid", SqlDbType.Int);
            param.Value = int.Parse(txtOrderID.Text);
            cmd.Parameters.Add(param);
            param = new SqlParameter("@custid", SqlDbType.Int);
            param.Value = int.Parse(ddlOrderCustomer.SelectedValue.ToString());
            cmd.Parameters.Add(param);
            param = new SqlParameter("@empid", SqlDbType.Int);
            param.Value = int.Parse(ddlOrderEmployee.SelectedValue.ToString());
            cmd.Parameters.Add(param);
            DateTime dt;
            param = new SqlParameter("@orderdate", SqlDbType.DateTime);
            DateTime.TryParse(txtOrderDate.Text, out dt);
            param.Value = dt.ToShortDateString();
            cmd.Parameters.Add(param);
            param = new SqlParameter("@requireddate", SqlDbType.DateTime);
            DateTime.TryParse(txtRequiredDate.Text, out dt);
            param.Value = dt.ToShortDateString();
            cmd.Parameters.Add(param);
            param = new SqlParameter("@shippeddate", SqlDbType.DateTime);
            if (ckbShipperDate.Checked == true)
            {
                DateTime.TryParse(txtShippedDate.Text, out dt);
                param.Value = dt.ToShortDateString();
            }
            else
            {
                param.Value = System.Convert.DBNull;
            }
            cmd.Parameters.Add(param);
            param = new SqlParameter("@shipperid", SqlDbType.Int);
            param.Value = int.Parse(ddlShipperid.SelectedValue.ToString());
            cmd.Parameters.Add(param);
            param = new SqlParameter("@freight", SqlDbType.Money);
            param.Value = decimal.Parse(txtFreight.Text);
            cmd.Parameters.Add(param);
            param = new SqlParameter("@shipname", SqlDbType.NVarChar, 40);
            param.Value = txtShipname.Text;
            cmd.Parameters.Add(param);
            param = new SqlParameter("@shipaddress", SqlDbType.NVarChar, 60);
            param.Value = txtShipaddress.Text;
            cmd.Parameters.Add(param);
            param = new SqlParameter("@shipcity", SqlDbType.NVarChar, 15);
            param.Value = txtShipcity.Text;
            cmd.Parameters.Add(param);
            param = new SqlParameter("@shipregion", SqlDbType.NVarChar, 15);
            param.Value = txtShipRegion.Text;
            cmd.Parameters.Add(param);
            param = new SqlParameter("@shippostalcode", SqlDbType.NVarChar, 10);
            param.Value = txtShipcode.Text;
            cmd.Parameters.Add(param);
            param = new SqlParameter("@shipcountry", SqlDbType.NVarChar, 15);
            param.Value = ddlShipCountry.Text;
            cmd.Parameters.Add(param);
            cmd.ExecuteNonQuery();
            Response.Write("<script language='JavaScript'> alert('Update successful!'); </script>");
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            con.Close();
        }
    }

    void DeleteOrder()
    {
        try
        {
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "Sales.Orders_Delete";
            SqlParameter param = new SqlParameter("@orderid", SqlDbType.Int);
            param.Value = int.Parse(txtOrderID.Text);
            cmd.Parameters.Add(param);
            cmd.ExecuteNonQuery();
            Response.Write("<script language='JavaScript'> alert('Delete successful!'); </script>");
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            con.Close();
        }
    }

    void checkshippedday()
    {
        if (ckbShipperDate.Checked == true)
        {
            txtShippedDate.Enabled = true;
            btnUpdate.Enabled = false;
            btnOrderDAdd.Enabled = false;
            btnOrderDUpdate.Enabled = false;
            btnOrderDDelete.Enabled = false;
            btnOrderDNew.Enabled = false;
        }
        else
        {
            txtShippedDate.Enabled = false;
            btnOrderDAdd.Enabled = true;
            btnUpdate.Enabled = true;
            btnOrderDUpdate.Enabled = false;
            btnOrderDDelete.Enabled = false;
            btnOrderDNew.Enabled = true;
        }
    }

    void searchorder(string search)
    {
        if (search.Length == 0)
        {
            Session["Order_Search"] = false;
            LoadOrderInfo();
            defaultOrderform();
        }
        else
        {
            LoadOrderInfo();
            GridViewRow dr;
            bool show;
            for (int i = 0; i < gvOrders.Rows.Count; i++)
            {
                show = false;
                dr = gvOrders.Rows[i];
                dr.Visible = true;
                for (int j = 0; j < 14; j++)
                {
                    dr.Cells[j].BackColor = Color.White;
                    if (Server.HtmlDecode(dr.Cells[j].Text).ToLower().Contains(search.ToLower()) == true)
                    {
                        dr.Cells[j].BackColor = Color.Yellow;
                        show = true;
                    }
                }
                if (show == false)
                {
                    dr.Visible = false;
                }
            }
        }
    }

    void checksearch()
    {
        if ((bool)Session["Order_Search"] == true)
        {
            gvOrders.DataBind();
            searchorder(Session["Order_searchString"].ToString());
        }
        else
        {
            LoadOrderInfo();
        }
    }
    #endregion

    #region Action Order
    protected void Page_Load(object sender, EventArgs e)
    {
        con.ConnectionString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        if (this.IsPostBack == false)
        {
            LoadOrderInfo();
            defaultOrderform();
            Loadallcombobox();
            btnAdd.Enabled = true;
            btnUpdate.Enabled = false;
            btnDelete.Enabled = false;
            Session["Order_Search"] = false;
        }
        gvDetails.AllowPaging = true;
        gvDetails.PageSize = 3;
    }
    protected void btnNew_Click(object sender, EventArgs e)
    {
        Session["Order_Search"] = false;
        checksearch();
        if (this.IsPostBack)
        {
            defaultOrderform();
        }
        gvOrders.DataBind();
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        InsertnewOrder();
        checksearch();
        defaultOrderform();
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        UpdateOrder();
        checksearch();
        if (this.IsPostBack)
        {
            btnAdd.Enabled = true;
            btnUpdate.Enabled = false;
            btnDelete.Enabled = false;
        }
        defaultOrderform();
    }
    protected void gvOrders_SelectedIndexChanged(object sender, EventArgs e)
    {
        checksearch();
        defaultOrderform();
        if (this.IsPostBack)
        {
            btnAdd.Enabled = false;
            btnUpdate.Enabled = true;
            btnDelete.Enabled = true;
        }
        GridViewRow dg = gvOrders.SelectedRow;
        txtOrderID.Text = dg.Cells[1].Text;
        ddlOrderCustomer.SelectedItem.Text = Server.HtmlDecode(dg.Cells[2].Text);
        ddlOrderEmployee.SelectedItem.Text = Server.HtmlDecode(dg.Cells[3].Text);
        DateTime dt;
        DateTime.TryParse(dg.Cells[4].Text, out dt);
        txtOrderDate.Text = dt.ToString("yyyy/MM/dd");
        DateTime.TryParse(dg.Cells[5].Text, out dt);
        txtRequiredDate.Text = dt.ToString("yyyy/MM/dd");
        if (Server.HtmlDecode(dg.Cells[6].Text).Length == 1)
        {
            ckbShipperDate.Checked = false;
            ckbShipperDate.Enabled = true;
        }
        else
        {
            DateTime.TryParse(dg.Cells[6].Text, out dt);
            txtShippedDate.Text = Server.HtmlDecode(dt.ToString("yyyy/MM/dd"));
            ckbShipperDate.Checked = true;
            ckbShipperDate.Enabled = false;
        }
        ddlShipperid.SelectedItem.Text = Server.HtmlDecode(dg.Cells[7].Text);
        txtFreight.Text = Server.HtmlDecode(dg.Cells[8].Text);
        txtShipname.Text = Server.HtmlDecode(dg.Cells[9].Text);
        txtShipaddress.Text = Server.HtmlDecode(dg.Cells[10].Text);
        txtShipcity.Text = Server.HtmlDecode(dg.Cells[11].Text);
        txtShipRegion.Text = Server.HtmlDecode(dg.Cells[12].Text);
        txtShipcode.Text = Server.HtmlDecode(dg.Cells[13].Text);
        txtOrderDid.Text = txtOrderID.Text;
        ddlShipCountry.SelectedItem.Text = Server.HtmlDecode(dg.Cells[14].Text);
        checkshippedday();
        Session["OrderD_loadproduct"] = true;
        LoadOrderDetailsInfo(txtOrderID.Text);
        defaultOrderDetailsform();
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        try
        {
            DeleteOrder();
            checksearch();
            if (this.IsPostBack)
            {
                btnAdd.Enabled = true;
                btnUpdate.Enabled = false;
                btnDelete.Enabled = false;
            }
            defaultOrderform();
        }
        catch
        {
            Response.Write("<script language='JavaScript'> alert('You can not delete this order'); </script>");
            checksearch();
        }
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Session["Order_Search"] = true;
        Session["Order_searchString"] = txtSearch.Text;
        searchorder(Session["Order_searchString"].ToString());
    }
    #endregion

    #region Code Order Detail
    void LoadNewProductID()
    {
        SqlDataAdapter da = new SqlDataAdapter("SELECT productid,productname FROM Production.Products", con);
        DataTable dt = new DataTable("Products");
        da.Fill(dt);
        cmbOrderDProductID.DataValueField = "productid";
        cmbOrderDProductID.DataTextField = "productname";
        cmbOrderDProductID.DataSource = dt;
        cmbOrderDProductID.DataBind();
    }

    void LoadProduct_selected(string id)
    {
        cmbOrderDProductID.DataSource = null;
        string str = string.Format("SELECT a.*,b.productname AS Name,b.productid AS ID FROM Sales.OrderDetails a, Production.Products b WHERE '{0}'=a.orderid AND a.productid=b.productid", id);
        SqlDataAdapter da = new SqlDataAdapter(str, con);
        DataTable dd = new DataTable("Products Select");
        da.Fill(dd);
        cmbOrderDProductID.DataValueField = "ID";
        cmbOrderDProductID.DataTextField = "Name";
        cmbOrderDProductID.DataSource = dd;
        cmbOrderDProductID.DataBind();
    }

    void defaultOrderDetailsform()
    {
        checksearch();
        if (txtOrderDid.Text.Length == 0)
        {
            return;
        }
        LoadOrderDetailsInfo(txtOrderDid.Text);
        //gvDetails.DataSource = null;
        cmbOrderDProductID.DataSource = null;
        txtOrderDPrice.Text = "";
        numOrderDQuantity.Text = "1";
        numOrderDDiscount.Text = "0";
        txtOrderDTotal.Text = "";
        if (this.IsPostBack)
        {
            DateTime dt = DateTime.Parse(txtShippedDate.Text);
            DateTime now = DateTime.Now;
            if (ckbShipperDate.Checked == true && (now-dt).Days>1)
            {
                btnUpdate.Enabled = false;
            }
            else
            {
                btnUpdate.Enabled = true;
            }
        }
        gvDetails.DataBind();
        cmbOrderDProductID.DataBind();
    }

    void LoadOrderDetailsInfo(string orderid)
    {
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "Sales.OrderDetails_get";
        SqlParameter param = new SqlParameter("@Orderid", SqlDbType.Int);
        param.Value = int.Parse(orderid);
        cmd.Parameters.Add(param);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        LoadProduct_selected(orderid);
        txtOrderDid.Text = txtOrderID.Text;
        gvDetails.AutoGenerateSelectButton = true;
        gvDetails.AllowPaging = true;
        gvDetails.PageSize = 3;
        gvDetails.DataSource = dt;
        gvDetails.DataBind();
    }

    void SearchOrderDetail(string Orderid, string productid)
    {
        try
        {
            checksearch();
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "Sales.OrderDetails_search";
            SqlParameter param = new SqlParameter("@Orderid", SqlDbType.Int);
            param.Value = int.Parse(Orderid);
            cmd.Parameters.Add(param);
            param = new SqlParameter("@Productid", SqlDbType.Int);
            param.Value = int.Parse(productid);
            cmd.Parameters.Add(param);
            SqlDataReader dd = cmd.ExecuteReader();
            if (dd.Read() == true)
            {
                txtOrderDPrice.Text = dd[2].ToString();
                numOrderDQuantity.Text = dd[3].ToString();
                numOrderDDiscount.Text = "" + (double.Parse(dd[4].ToString()) * 100);
                double price = double.Parse(txtOrderDPrice.Text);
                int quantity = int.Parse(numOrderDQuantity.Text);
                double total = price * (quantity) - (double.Parse(dd[4].ToString()) * price * quantity);
                txtOrderDTotal.Text = "" + total;
            }
            dd.Close();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            con.Close();
            con.Open();
        }
    }

    void SearchPriceDetail(string Productid)
    {
        try
        {
            checksearch();
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "Sales.OrderDetails_searchprice";
            SqlParameter param = new SqlParameter("@Productid", SqlDbType.Int);
            param.Value = int.Parse(Productid);
            cmd.Parameters.Add(param);
            SqlDataReader dd = cmd.ExecuteReader();
            if (dd.Read() == true)
            {
                txtOrderDPrice.Text = dd[0].ToString();
            }
            dd.Close();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            con.Close();
            con.Open();
        }
    }

    void InsertOrderDetailInfo()
    {
        try
        {
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "Sales.OrderDetails_Insert";
            SqlParameter param = new SqlParameter("@Orderid", SqlDbType.Int);
            param.Value = int.Parse(txtOrderDid.Text);
            cmd.Parameters.Add(param);
            param = new SqlParameter("@Productid", SqlDbType.Int);
            param.Value = int.Parse(cmbOrderDProductID.SelectedValue.ToString());
            cmd.Parameters.Add(param);
            param = new SqlParameter("@UnitPrice", SqlDbType.Money);
            param.Value = decimal.Parse(txtOrderDPrice.Text);
            cmd.Parameters.Add(param);
            param = new SqlParameter("@Quantity", SqlDbType.SmallInt);
            param.Value = short.Parse(numOrderDQuantity.Text);
            cmd.Parameters.Add(param);
            param = new SqlParameter("@Discount", SqlDbType.Decimal);
            param.Precision = 4;
            param.Scale = 3;
            param.Value = decimal.Parse("" + (double.Parse(numOrderDDiscount.Text) / 100));
            cmd.Parameters.Add(param);
            cmd.ExecuteNonQuery();
            LoadProduct_selected(txtOrderDid.Text);
            Response.Write("<script language='JavaScript'> alert('Insert successful!'); </script>");
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            con.Close();
        }
    }

    void UpdateOrderDetailInfo()
    {
        try
        {
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "Sales.OrderDetails_Update";
            SqlParameter param = new SqlParameter("@Orderid", SqlDbType.Int);
            param.Value = int.Parse(txtOrderDid.Text);
            cmd.Parameters.Add(param);
            param = new SqlParameter("@Productid", SqlDbType.Int);
            param.Value = int.Parse(cmbOrderDProductID.SelectedValue.ToString());
            cmd.Parameters.Add(param);
            param = new SqlParameter("@UnitPrice", SqlDbType.Money);
            param.Value = decimal.Parse(txtOrderDPrice.Text);
            cmd.Parameters.Add(param);
            param = new SqlParameter("@Quantity", SqlDbType.SmallInt);
            param.Value = short.Parse(numOrderDQuantity.Text);
            cmd.Parameters.Add(param);
            param = new SqlParameter("@Discount", SqlDbType.Decimal);
            param.Precision = 4;
            param.Scale = 3;
            param.Value = decimal.Parse("" + (double.Parse(numOrderDDiscount.Text) / 100));
            cmd.Parameters.Add(param);
            cmd.ExecuteNonQuery();
            LoadProduct_selected(txtOrderDid.Text);
            Response.Write("<script language='JavaScript'> alert('Update successful!'); </script>");
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            con.Close();
            con.Open();
        }
    }

    void DeleteOrderDetailInfo()
    {
        try
        {
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "Sales.OrderDetails_Delete";
            SqlParameter param = new SqlParameter("@Orderid", SqlDbType.Int);
            param.Value = int.Parse(txtOrderDid.Text);
            cmd.Parameters.Add(param);
            param = new SqlParameter("@Productid", SqlDbType.Int);
            param.Value = int.Parse(cmbOrderDProductID.SelectedValue.ToString());
            cmd.Parameters.Add(param);
            cmd.ExecuteNonQuery();
            LoadProduct_selected(txtOrderDid.Text);
            Response.Write("<script language='JavaScript'> alert('Delete successful!'); </script>");
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            con.Close();
            con.Open();
        }
    }

    #endregion

    #region Action Order Datails
		
    protected void gvDetails_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        checksearch();
        LoadOrderDetailsInfo(txtOrderDid.Text);
        gvDetails.PageIndex = e.NewPageIndex;
        gvDetails.DataBind();
    }

    protected void cmbOrderDProductID_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            if (cmbOrderDProductID.Text.Length > 0 && txtOrderDid.Text.Length > 0 && (bool)Session["OrderD_loadproduct"]==true)
            {
                SearchOrderDetail(txtOrderDid.Text, cmbOrderDProductID.SelectedValue.ToString());
            }
            if (cmbOrderDProductID.Text.Length > 0 && txtOrderDid.Text.Length > 0 && (bool)Session["OrderD_loadproduct"] == false)
            {
                SearchPriceDetail(cmbOrderDProductID.SelectedValue.ToString());
            }
            checksearch();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    protected void btnOrderDNew_Click(object sender, EventArgs e)
    {
        Session["OrderD_loadproduct"] = false;
        defaultOrderDetailsform();
        LoadNewProductID();
        cmbOrderDProductID.DataBind();
        if (ckbShipperDate.Checked == false)
        {
            btnOrderDAdd.Enabled = true;
            btnOrderDDelete.Enabled = false;
            btnOrderDUpdate.Enabled = false;
        }
    }
    protected void btnOrderDAdd_Click(object sender, EventArgs e)
    {
        try
        {
            InsertOrderDetailInfo();
            checksearch();
            if (txtOrderDid.Text.Length == 0)
            {
                return;
            }
            LoadOrderDetailsInfo(txtOrderDid.Text);
            Session["OrderD_loadproduct"] = true;
        }
        catch (Exception)
        {
            Response.Write("<script language='JavaScript'> alert('Product exist! You can not insert this product'); </script>");
            checksearch();
        }
    }
    protected void btnOrderDUpdate_Click(object sender, EventArgs e)
    {
        UpdateOrderDetailInfo();
        checksearch();
        if (txtOrderDid.Text.Length == 0)
        {
            return;
        }
        LoadOrderDetailsInfo(txtOrderDid.Text);
        Session["OrderD_loadproduct"] = true;
    }
    protected void btnOrderDDelete_Click(object sender, EventArgs e)
    {
        DeleteOrderDetailInfo();
        checksearch();
        if (txtOrderDid.Text.Length == 0)
        {
            return;
        }
        LoadOrderDetailsInfo(txtOrderDid.Text);
        Session["OrderD_loadproduct"] = true;
    }
    protected void gvDetails_SelectedIndexChanged(object sender, EventArgs e)
    {
        checksearch();
        LoadOrderDetailsInfo(txtOrderDid.Text);
        if (ckbShipperDate.Checked == false)
        {
            btnOrderDAdd.Enabled = false;
            btnOrderDDelete.Enabled = true;
            btnOrderDUpdate.Enabled = true;
        }
        GridViewRow dg = gvDetails.SelectedRow;
        cmbOrderDProductID.SelectedValue = dg.Cells[2].Text;
        txtOrderDPrice.Text = dg.Cells[3].Text;
        numOrderDQuantity.Text = dg.Cells[4].Text;
        numOrderDDiscount.Text = "" + (double.Parse(dg.Cells[5].Text) * 100);
        double price = double.Parse(txtOrderDPrice.Text);
        int quantity = int.Parse(numOrderDQuantity.Text);
        double total = price * (quantity) - (double.Parse(dg.Cells[5].Text) * price * quantity);
        txtOrderDTotal.Text = "" + total;
        Session["OrderD_loadproduct"] = true;
        gvOrders.DataBind();
        gvDetails.DataBind();
    }

	#endregion

}
